using Senparc.Weixin.Exceptions;
using Senparc.Weixin.QY.Entities;
using Senparc.Weixin.QY.Helpers;
using System;
using System.Xml.Linq;
namespace Senparc.Weixin.QY
{
	public static class ResponseMessageFactory
	{
		public static IResponseMessageBase GetResponseEntity(XDocument doc)
		{
			ResponseMessageBase responseMessageBase = null;
			try
			{
				ResponseMsgType responseMsgType = MsgTypeHelper.GetResponseMsgType(doc);
				switch (responseMsgType)
				{
				case ResponseMsgType.Text:
					responseMessageBase = new ResponseMessageText();
					goto IL_6E;
				case ResponseMsgType.News:
					responseMessageBase = new ResponseMessageNews();
					goto IL_6E;
				case ResponseMsgType.Image:
					responseMessageBase = new ResponseMessageImage();
					goto IL_6E;
				case ResponseMsgType.Voice:
					responseMessageBase = new ResponseMessageVoice();
					goto IL_6E;
				case ResponseMsgType.Video:
					responseMessageBase = new ResponseMessageVideo();
					goto IL_6E;
				}
				throw new UnknownRequestMsgTypeException(string.Format("MsgType：{0} 在ResponseMessageFactory中没有对应的处理程序！", responseMsgType), new ArgumentOutOfRangeException());
				IL_6E:
				responseMessageBase.FillEntityWithXml(doc);
			}
			catch (ArgumentException inner)
			{
				throw new WeixinException(string.Format("ResponseMessage转换出错！可能是MsgType不存在！，XML：{0}", doc.ToString()), inner);
			}
			return responseMessageBase;
		}
		public static IResponseMessageBase GetResponseEntity(string xml)
		{
			return ResponseMessageFactory.GetResponseEntity(XDocument.Parse(xml));
		}
	}
}
